package com.lxm.enigma.filter;

import com.lxm.enigma.jsonresult.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: Lys
 * @Date 2022/6/2
 * @Describe
 **/
@Slf4j
@RestControllerAdvice//(basePackages = "com.lxm")
public class EnigmaInvalidAdvice {


    /**
     * 拦截EnigmaRequestAdvice里，返回body为空的错误。
     * EnigmaRequestAdvice里如果直接抛错，会在servlet-filter里处理掉，无法被这里拦截, 因此返回空，让servlet-filter抛出HttpMessageNotReadableException，再到这里处理
     *
     * @param req
     * @param res
     * @param e
     * @return
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public JsonResult<?> invalidSignCipherExceptionCatch(HttpServletRequest req, HttpServletResponse res, HttpMessageNotReadableException e) {
        log.error("global exception handler ,uri : {}, exception : {}", req.getRequestURI(), e.getMessage());
        //res.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        return JsonResult.json(-9, "incorrect cipher");
    }

}
